import filemanager.CopyOperation;
import filemanager.DeleteOperation;
import filesystem.FileNode;
import filesystem.TreeTransferHandler;

import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;

public class FileManagerGUI {
    private JFrame frame;
    private JTree fileTree;
    private DefaultTreeModel treeModel;

    public void launch() {
        frame = new JFrame("Java File Manager");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        FileNode rootNode = new FileNode("目录", true);

        FileNode folder1 = new FileNode("文档", true);
        folder1.add(new FileNode("文档1.docx", false));
        folder1.add(new FileNode("文档2.pdf", false));

        FileNode folder2 = new FileNode("图片", true);
        folder2.add(new FileNode("图片1.jpg", false));
        folder2.add(new FileNode("图片2.png", false));

        rootNode.add(folder1);
        rootNode.add(folder2);

        treeModel = new DefaultTreeModel(rootNode);
        fileTree = new JTree(treeModel);

        fileTree.setDragEnabled(true);
        fileTree.setDropMode(DropMode.ON_OR_INSERT);
        fileTree.setTransferHandler(new TreeTransferHandler(treeModel, frame));

        JScrollPane treeScroll = new JScrollPane(fileTree);

        JButton copyBtn = new JButton("Copy");
        JButton deleteBtn = new JButton("Delete");

        copyBtn.addActionListener(e -> CopyOperation.performCopy(fileTree, treeModel, frame));
        deleteBtn.addActionListener(e -> DeleteOperation.performDelete(fileTree, treeModel, frame));

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(copyBtn);
        buttonPanel.add(deleteBtn);

        frame.getContentPane().add(treeScroll, BorderLayout.CENTER);
        frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        frame.setVisible(true);
    }
}
